import java.util.Scanner;

public class LinkedListStackDemo {
    public static void main(String[] args) {
        System.out.println("输入栈的大小:");
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        LinkedListStack stack = new LinkedListStack(size);
/*        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.list();
        System.out.println("栈的大小为:" + stack.size());


        System.out.println("出栈的数据是:" + stack.pop());
        System.out.println("出栈的数据是:" + stack.pop());

        stack.list();*/

        String key = "";
        boolean loop = true; //控制是否退出菜单


        while (loop) {
            System.out.println("show: 表示显示栈");
            System.out.println("push: 表示添加数据到栈(入栈)");
            System.out.println("pop:  表示从栈取出数据(出栈)");
            System.out.println("exit: 退出程序");
            System.out.println("请输入你的选择");
            key = scanner.next();
            switch (key) {
                case "show":
                    stack.list();
                    break;
                case "push":
                    System.out.println("请输入一个数");
                    int value = scanner.nextInt();
                    stack.push(value);
                    break;
                case "pop":
                    try {
                        int res = stack.pop();
                        System.out.printf("出栈的数据是 %d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case "exit":
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~~");
    }
}

class LinkedListStack {

    private ListNode top;//栈顶
    private int size;//栈的大小

    public LinkedListStack(int size) {
        this.size = size;
    }

    //栈空
    public boolean isEmpty() {
        return top == null;
    }

    //栈的大小
    public int size() {
        int len = 0;
        ListNode cur = top;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    //入栈
    public void push(int value) {
        if (size == size()) {
            System.out.println("栈满无法添加！");
            return;
        }
        if (size() != size) {
            //头插法
            ListNode node = new ListNode(value);
            if (top == null) {
                top = node;
                return;
            }
            node.next = top;
            top = node;
        }
    }

    //出栈
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("当前栈为空！");
        }
        int value = top.value;
        top = top.next;
        return value;
    }

    //打印栈
    public void list() {
        if (top == null) {
            System.out.println("当前栈为空！");
        }
        //从栈顶开始遍历
        ListNode cur = top;
        while (cur != null) {
            System.out.println("stack:" + cur.value);
            cur = cur.next;
        }

    }

}

class ListNode {
    public int value;
    public ListNode next;

    public ListNode(int value) {
        this.value = value;
    }

    public ListNode() {
    }
}
